{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.c1b511d2019021c84023040bba3fca5d.315360000.1918884397.282335-22869417', 'expires_in': 2592000, 'session_key': '9mzdXRZAdN4ugEn8v8jz+7btfVAJxL0Q7t79zBbOnGGQOScge/7HpbznNoOMG7hfQez1IVOBYQeSFHyjyOSCAtd0cggrcw==', 'access_token': '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': '222b2f2e50726399bffd49921a284f9e'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=s0Osp1GRugjG4l3HZYhxK1Yf&client_secret=PGsx5lBrAo445lqEclFOasNE7BvVVISZ'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 525652545656, 'timestamp': 1603527174, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': 'b4891643b392cd924b1eda7fc6baa6c4', 'location': {'left': 736.31, 'top': 246.11, 'width': 308, 'height': 318, 'rotation': 3}, 'face_probability': 1, 'angle': {'yaw': 31.73, 'pitch': 3.39, 'roll': 3.39}, 'face_shape': {'type': 'oval', 'probability': 0.57}, 'face_type': {'type': 'human', 'probability': 0.96}}]}}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "\n",
    "#人脸检测与属性分析\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "params = \"{\\\"image\\\":\\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603530792718&di=570db09e90c2680ca391c692b305ab32&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2Fe5cea2e8cc729a2118beae3d87f60968c6a97253.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "# image_type 图片类型：BASE64；URL；FACE_TOKEN。\n",
    "access_token = '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 2014575755555, 'timestamp': 1603528770, 'cached': 0, 'result': {'score': 90.17651367, 'face_list': [{'face_token': '5e2bb5741716a7bba9527de9a6b27d94'}, {'face_token': '275836d75526713baed8ea955359ad3b'}]}}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "# 人脸对比\n",
    "\n",
    "import requests\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "params = \"[{\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603538818977&di=c1f7eb563e4390c99c10a76887138d1a&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F29%2F20161029085449_Bh3aW.jpeg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3165065607,4294731690&fm=26&gp=0.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "\n",
    "access_token = '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 5510120184350, 'timestamp': 1603529224, 'cached': 0, 'result': None}\n"
     ]
    }
   ],
   "source": [
    "# 3.创建用户列表\n",
    "\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group1\\\"}\"\n",
    "access_token = '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 9445001650012, 'timestamp': 1603529862, 'cached': 0, 'result': {'face_token': '275836d75526713baed8ea955359ad3b', 'location': {'left': 166.97, 'top': 445.82, 'width': 138, 'height': 141, 'rotation': -1}}}\n"
     ]
    }
   ],
   "source": [
    "# 人脸注册\n",
    "import requests\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"275836d75526713baed8ea955359ad3b\\\",\\\"image_type\\\":\\\"FACE_TOKEN\\\",\\\"group_id\\\":\\\"group1\\\",\\\"user_id\\\":\\\"user1\\\",\\\"user_info\\\":\\\"abc\\\",\\\"quality_control\\\":\\\"LOW\\\",\\\"liveness_control\\\":\\\"LOW\\\"}\"\n",
    "access_token = '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 8945057500189, 'timestamp': 1603529970, 'cached': 0, 'result': {'face_token': '275836d75526713baed8ea955359ad3b', 'location': {'left': 166.97, 'top': 445.82, 'width': 138, 'height': 141, 'rotation': -1}}}\n"
     ]
    }
   ],
   "source": [
    "#人脸更新\n",
    "import requests\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/update\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"275836d75526713baed8ea955359ad3b\\\",\\\"image_type\\\":\\\"FACE_TOKEN\\\",\\\"group_id\\\":\\\"group1\\\",\\\"user_id\\\":\\\"user1\\\",\\\"user_info\\\":\\\"cba\\\",\\\"quality_control\\\":\\\"LOW\\\",\\\"liveness_control\\\":\\\"LOW\\\"}\"\n",
    "access_token = '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 8479159435051, 'timestamp': 1603530049, 'cached': 0, 'result': {'face_list': [{'face_token': '275836d75526713baed8ea955359ad3b', 'ctime': '2020-10-24 16:59:30'}]}}\n"
     ]
    }
   ],
   "source": [
    "#获取用户人脸列表\n",
    "import requests\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist\"\n",
    "\n",
    "params = \"{\\\"user_id\\\":\\\"user1\\\",\\\"group_id\\\":\\\"group1\\\"}\"\n",
    "access_token = '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 8489450515156, 'timestamp': 1603530122, 'cached': 0, 'result': None}\n"
     ]
    }
   ],
   "source": [
    "#删除用户组\n",
    "import requests\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/delete\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group1\\\"}\"\n",
    "access_token = '24.fbdfb67868614c510163129b354e0509.2592000.1606116397.282335-22869417'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
